package kis.io.xml.bysocket.apply;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.Socket;

public class TestPost
{
	
	public static void main(String[] args) throws Exception
	{
		String ip = "127.0.0.1";
		int port = 8099;
		Socket socket = new Socket(ip,port);
		OutputStream out=socket.getOutputStream();
		 File file = new File("E:\\work\\workspace\\WGJACT\\src\\FindCar.xml");  //用于发送的文件路径
		 String data="";
		 PrintWriter pw=new PrintWriter(out);
		 System.out.println(getXML(file));
		 /**
		  * 可以分次发送 第一次先发送 长度  第二次发送正文
		  */
		 pw.print("0000000280");
		 pw.print(getXML(file));
		 pw.flush();
		 InputStream is = socket.getInputStream();
		String msg="";
		byte[] b = new byte[10];
		is.read(b);
		String xmlSize=new String(b);
		int length = Integer.parseInt(xmlSize);
		byte[] xmlContent = new byte[length];
		is.read(xmlContent);
		System.out.println(new String(xmlContent,"GBK"));
	}
	
	
	
	public static String getXML(File file){
		String xml = "";
		try
		{
			try
			{
				InputStreamReader read=new InputStreamReader(new FileInputStream(file),"GBK");
				BufferedReader reader = new BufferedReader(read);
				String line="";
				try
				{
					while((line=reader.readLine())!=null)
					{
						xml+=line;
					}
				} catch (IOException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} catch (UnsupportedEncodingException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (FileNotFoundException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return xml;
	}
}
